package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmGeofenceEntity;
import com.kaili.basic.pojo.entity.MdmRailEntity;
import com.kaili.basic.pojo.entity.MdmTimefenceEntity;
import com.kaili.basic.service.IBaseCityService;
import com.kaili.basic.service.IMdmRailService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
*  围栏管理控制器
* @author:nigang
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "围栏管理", description = "围栏管理相关设置API")
public class MdmRailController {
    @Autowired
	IMdmRailService mdmRailService;

	@Autowired
	private IBaseCityService baseCityService;

    @PostMapping(value = "/rails/geofence")
    @ApiOperation("新增地理围栏")
    public RestStatus addGeofence(@RequestBody MdmGeofenceAddDto dto) {
    	if(mdmRailService.queryGeofenceByName(dto.getRailName())!=null){
			return CommonStatus.RAIL_NAME_REPETITION;
		}
        mdmRailService.addGeofence(dto);
        return CommonStatus.ADD_OK;
    }

	@PostMapping(value = "/rails/timefence")
	@ApiOperation("新增时间围栏")
	public RestStatus addTimefence(@RequestBody MdmTimefenceAddDto dto) throws Exception {
		if(mdmRailService.queryTimefenceByName(dto.getRailName())!=null){
			return CommonStatus.RAIL_NAME_REPETITION;
		}
		mdmRailService.addTimefence(dto);
		return CommonStatus.ADD_OK;
	}

    @DeleteMapping(value = "/rails/{id}")
    @ApiOperation("删除")
    public RestStatus del(@ApiParam("ID") @PathVariable("id") Integer id) {
        mdmRailService.deleteRail(id);
        return CommonStatus.DELETE_OK;
    }

	@PutMapping(value = "/rails/geofence")
	@ApiOperation("修改地理围栏")
	public RestStatus updateGeofence(@RequestBody MdmGeofenceUpdateDto dto) {
		MdmGeofenceEntity entity=mdmRailService.queryGeofenceByName(dto.getRailName());
		if(entity!=null){
			if(entity.getId().longValue()!=dto.getId().longValue()) {
				return CommonStatus.RAIL_NAME_REPETITION;
			}
		}
		mdmRailService.updateGeofence(dto);
		return CommonStatus.UPDATE_OK;
	}

	@PutMapping(value = "/rails/timefence")
	@ApiOperation("修改时间围栏")
	public RestStatus updateTimefence(@RequestBody MdmTimefenceUpdateDto dto) throws Exception {
		MdmTimefenceEntity entity=mdmRailService.queryTimefenceByName(dto.getRailName());
		if(entity!=null){
			if(entity.getId().longValue()!=dto.getId().longValue()) {
				return CommonStatus.RAIL_NAME_REPETITION;
			}
		}
		mdmRailService.updateTimefence(dto);
		return CommonStatus.UPDATE_OK;
	}

	@GetMapping(value = "/rails/{id}/geofence")
	@ApiOperation("根据id查询地理围栏")
	public MdmGeofenceEntity queryGeofenceById(@ApiParam("ID") @PathVariable("id") Integer id) {
		MdmGeofenceEntity entity=mdmRailService.queryGeofenceById(id);
		MdmCityInfo info=baseCityService.getByCountyId(entity.getCountyId().longValue());
		if(info!=null){
			Long[] ids=new Long[3];
			ids[0]=info.getProvinceId();
			ids[1]=info.getCityId();
			ids[2]=info.getCountyId();
			String[] names=new String[3];
			names[0]=info.getProvinceName();
			names[1]=info.getCityName();
			names[2]=info.getCountyName();
			entity.setIds(ids);
			entity.setNames(names);
		}
		return entity;
	}

	@GetMapping(value = "/rails/{id}/timefence")
	@ApiOperation("根据id查询时间围栏")
	public MdmTimefenceEntity queryTimefenceById(@ApiParam("ID") @PathVariable("id") Integer id) throws Exception {
		return mdmRailService.queryTimefenceById(id);
	}

	@GetMapping(value = "/rails")
	@ApiOperation("分页查询")
	public Page<MdmRailEntity> queryPageList(@ModelAttribute MdmRailQueryDto dto) {
		return mdmRailService.queryRail(new Page<MdmRailEntity>(dto.getPage(),dto.getPageSize()),dto);
	}

	@GetMapping(value = "/rails/geofenceAll")
	@ApiOperation("查询所有地理围栏")
	public List<MdmGeofenceEntity> queryGeofenceAll() {
		return mdmRailService.queryGeofenceAll();
	}

	@GetMapping(value = "/rails/timefenceAll")
	@ApiOperation("查询所有时间围栏")
	public List<MdmTimefenceEntity> queryTimefenceAll() {
		return mdmRailService.queryTimefenceAll();
	}

	/*@GetMapping(value = "/rails/import")
	@ApiOperation("导入")
	public RestStatus importExcel(@RequestParam("file") MultipartFile file) throws Exception {
		List<MdmGeofenceEntity> list = ExcelUtil.importExcel(file, 1, 2, MdmGeofenceEntity.class);
		for(MdmGeofenceEntity entity : list){
			this.add(entity);
		}

		return CommonStatus.OK;
	}

	@GetMapping(value = "/rails/export")
	@ApiOperation("导出")
	public void export(HttpServletResponse response, @ModelAttribute MdmGeofenceEntity dto) throws RestException {
		Wrapper<MdmGeofenceEntity> wrapper = new EntityWrapper<MdmGeofenceEntity>();
		List<MdmGeofenceEntity> list = mdmRailService.selectList(wrapper);
		ExcelUtil.exportExcel(list,"","sheet1",MdmGeofenceEntity.class, UUID.randomUUID().toString()+".xls",response);
	}*/
}
